package com.lagou;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import javax.servlet.ServletException;
import java.io.File;

//References:
//https://www.cnblogs.com/develon/p/11602969.html
//https://www.iteye.com/blog/weiqingfei-140816
//SpringBoot内置tomcat启动原理
//https://www.cnblogs.com/sword-successful/archive/2019/08/20/11383723.html
//Embed Tomcat8简单示例
//https://blog.csdn.net/guolong1983811/article/details/78535984
//https://morris131.blog.csdn.net/article/details/115367815?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link
public class TomcatServer {
    private static final String DEFAULT_BASEDIR = "D:/MyTemp/temp/tomcat";
    private static final String DEFAULT_HOST = "127.0.0.1";
    private static final int DEFAULT_PORT = 8080;
    private static final String DEFAULT_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";
    private static final String DEFAULT_CONTEXT = "/";

    private Tomcat tomcat;

    public TomcatServer() {
        tomcat = createTomcat();
    }

    public void start() {
        try {
            tomcat.start();
            tomcat.getServer().await();

        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    private Tomcat createTomcat() {
        tomcat = new Tomcat();
        tomcat.setPort(DEFAULT_PORT);
        try {
            tomcat.addWebapp("/",new File(".").getAbsolutePath()).setReloadable(false);
        } catch (ServletException e) {
            e.printStackTrace();
        }

        return tomcat;
    }

}
